﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QuanLyBus;

namespace testSMS
{
    //Class Go TV
    //Duoc xay dung boi : Nguyen Duy Phuong - Open University,
    // Email; duyphuong13@hotmail.com or hyesojong@yahoo.com
    public class classGoTV
    {
        private int _textchanged;
        private bool _conguyenam;
        private bool _xoaktu;
        private bool _nhapspace;
        private string _chuoi;   
        private int _selectionstart;
   
        
        public int SelectionStart
        {
            get { return _selectionstart; }
            set { _selectionstart = value; }
        }
        
    

        public int TextChanged
        {
            get { return _textchanged; }
            set { _textchanged = value; }
        }
        public bool CoNguyenAm
        {
            get { return _conguyenam; }
            set { _conguyenam = value; }
        }      
        public bool XoaKtu
        {
            get { return _xoaktu; }
            set { _xoaktu = value; }
        }
        public bool NhapSpace
        {
            get { return _nhapspace; }
            set { _nhapspace = value; }
            
        }
        public string Chuoi
        {
            get { return _chuoi; }
            set { _chuoi = value; }
        }

        public classGoTV()
        {
            _chuoi = "";
            _xoaktu = false;
            _nhapspace = false;
            _conguyenam = false;
            _textchanged = 1; 
        }

        private int vtriNguyenAm = -1;
        private char[] backup = new char[3];
        

        private bool TachChuoi(ref string Chuoi1, ref string Chuoi2) //return true thi` lam tiep, false thi k lam gi ca
        {
            int vt_khoangtrang;
            if (NhapSpace)
            {
                NhapSpace = false;
                Chuoi1 = Chuoi;
                Chuoi2 = "";
                CoNguyenAm = false;
                vtriNguyenAm = -1;
                backup[0] = 'f';
                return false;
            }
            else
            {
                vt_khoangtrang = Chuoi.LastIndexOf(' ');
                Chuoi1 = Chuoi.Substring(0, vt_khoangtrang + 1);
                Chuoi2 = Chuoi.Substring(vt_khoangtrang + 1, Chuoi.Length - 1 - vt_khoangtrang);
                return true;
            }
        }
        private bool xly_Xoaktu()
        {
            int vt_khoangtrang;
            string test = "";
            
            if (Chuoi == "")
            {
                CoNguyenAm = false;
                vtriNguyenAm = -1;
                return false;
            }
            else
            {
                vt_khoangtrang = Chuoi.LastIndexOf(' ');
               // Chuoi1 = chuoi.Substring(0, vt_khoangtrang + 1);
                test = Chuoi.Substring(vt_khoangtrang + 1,Chuoi.Length - 1 - vt_khoangtrang);
                if (test == "")
                {
                    CoNguyenAm = false;
                    vtriNguyenAm = -1;
                    return false;
                }
                else
                {
                  // tim vtri nguyen am gan nhat
                    for (int i = test.Length - 1; i >= 0; i--)
                    {
                        if (NguyenAmGanNhat(test[i]))
                        {
                            vtriNguyenAm = i;
                            CoNguyenAm = true;
                            return true;
                        }
                    }
                    CoNguyenAm = false;
                    vtriNguyenAm = -1;
                    return false;
                }
            }
        }

        private void XlyChuoi(ref string chuoi)
        {
            char key;

            try
            {
                key = chuoi[chuoi.Length - 1];
                if (chuoi.Length == 1) // neu chuoi chi co 1 ktu, ktra xem co phai la nguyen am k
                {
                    if (NguyenAm(key))
                    {
                        CoNguyenAm = true;
                        vtriNguyenAm = 0;
                    }
                    
                }
                else
                {
                    string kq = chuoi.Substring(0, chuoi.Length - 1);
                    if (backup[0] == 't'  &&
                        (
                        (DauDup(key) && backup[2] == key)  || 
                        (ThanhDau(key) && backup[2] == key) || 
                        (ThanhDau(key) && ThanhDau(backup[2]))
                         ))
                    {

                      
                       
                        if (DauDup(key) && backup[2] == key )
                        {
                            // backup lai chuoi vua nhap

                            kq = kq.Substring(0,kq.Length-1) + backup[1] + backup[2];
                            
                            chuoi = kq;
                            vtriNguyenAm++;
                            // fai check lai nguyen am gan nhat
                            backup[0] = 'f';
                            SelectionStart++;
                            
                            
                        }
                        else {
                            if (ThanhDau(key) && backup[2] == key)
                            {
                                
                                // bo? dau

                                    kq = kq.Substring(0, vtriNguyenAm) + backup[1] + kq.Substring(vtriNguyenAm + 1) + backup[2];
                                    chuoi = kq;

                                    backup[0] = 'f';
                                    SelectionStart++;
                                

                            }
                            else
                                if (ThanhDau(key) && ThanhDau(backup[2]))
                                {
                               

                                    if (key == 'z' || key=='Z') // bo dau
                                    {
                                        kq = kq.Substring(0, vtriNguyenAm) + backup[1] + kq.Substring(vtriNguyenAm + 1);
                                        chuoi = kq;
                                        backup[2] = key;
                                    }     // doi dau
                                    else
                                    {
                                        kq = kq.Substring(0, vtriNguyenAm) + App.Godau.TimKiem(backup[1], key) + kq.Substring(vtriNguyenAm + 1);
                                        chuoi = kq;
                                        backup[2] = key;
                                    }
                                    
                                 
                                }
                               

                        }
                       
                    }
                    else
                    {

                        if (DauDup(key) && truoc_DauDup(kq[kq.Length - 1])) // xu ly dau dup
                        {
                            //MessageBox.Show(truoc_DauDup(kq[kq.Length - 1]).ToString());
                            Xly_DauDup(key, ref kq);
                            chuoi = kq;
                            
                        }
                        else
                        {
                            if (CoNguyenAm && ThanhDau(key))// xu ly thanh dau
                            {
                                Xly_ThanhDau(key, ref kq);
                                chuoi = kq;
                            }
                            else
                            {
                                if (NguyenAm(key))
                                {
                                    CoNguyenAm = true;
                                    vtriNguyenAm = chuoi.Length - 1;
                                    backup[0] = 'f';
                                   
                                }
                            }

                        }
                    }


                }
            }
            catch
            {

            }


        }
        private bool Xly_ThanhDau(char key, ref string chuoi)
        {
           
            string kq = App.Godau.TimKiem(chuoi[vtriNguyenAm], key);
            if (kq != null)
            {

                backup[0] = 't';
                backup[1] = chuoi[vtriNguyenAm];
                backup[2] = key;
                chuoi = chuoi.Substring(0, vtriNguyenAm) + kq + chuoi.Substring(vtriNguyenAm + 1);

                return true;
            }
            chuoi += key;
            return false;
        }
        private bool Xly_DauDup(char key, ref string chuoi)
        {

            //ch == 'a' || ch == 'd' || ch == 'o' || ch == 'e' || ch == 'w'
            //(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y');

            string kq = App.Godau.TimKiem(chuoi[chuoi.Length - 1], key);
            if (kq != null)
            {
                backup[0] = 't';
                backup[1] = chuoi[chuoi.Length - 1];
                backup[2] = key;
                chuoi = chuoi.Substring(0, chuoi.Length - 1) + kq;
                vtriNguyenAm = chuoi.Length - 1;

                return true;
            }
            chuoi += key;
            vtriNguyenAm = chuoi.Length - 1;
            return false;

        }
       public bool GoDauTV(ref TextBox textBox)
        {

            SelectionStart = textBox.SelectionStart;

            if (textBox.SelectionStart == textBox.Text.Length)
            {
                Chuoi = textBox.Text;
            }
            else
                Chuoi = textBox.Text.Substring(0, SelectionStart);


              
                string Chuoi1 = "", Chuoi2 = "";

                if (XoaKtu)
                {
                    XoaKtu = false;
                    TextChanged = 0;
                    xly_Xoaktu();
                    backup[0] = 'f';
                    return false;
                }
                else
                {
                    if (Chuoi == "")

                        return false;

                    string chuoi2_old = "";
                    if (TachChuoi(ref Chuoi1, ref Chuoi2) == false)// Hàm TachChuoi trả về false thì chuỗi 1 gồm tất cả, chuỗi 2 rỗng
                    {

                        TextChanged = 0;

                        Chuoi = Chuoi1 + Chuoi2;
                        

                        textBox.Select(Chuoi.Length, 0);
                        return false;
                    }
                    else
                    {
                        chuoi2_old = Chuoi2;
                        XlyChuoi(ref Chuoi2); // ham chinh

                        textBox.Text = Chuoi1 + Chuoi2 + textBox.Text.Substring(textBox.SelectionStart);

                        if (chuoi2_old == Chuoi2)
                        {
                            TextChanged = 0;
                            textBox.Select(Chuoi.Length, 0);
                        }
                        else
                        {
                            TextChanged = -2;

                            textBox.Select(SelectionStart - 1, 0);
                        }

                    }

                    return true;
                
            }
          

            
          
          
       }

    





        
        private bool DauDup(char ch)
        {

            return (ch == 'a' || ch == 'd' || ch == 'o' || ch == 'e' || ch == 'w' ||
                ch == 'A' || ch == 'D' || ch == 'O' || ch == 'E' || ch == 'W');
        }
        private bool ThanhDau(char ch)
        {
            return (ch == 's' || ch == 'f' || ch == 'r' || ch == 'x' || ch == 'j' || ch == 'z' ||
                ch == 'S' || ch == 'F' || ch == 'R' || ch == 'X' || ch == 'J' || ch == 'Z');
        }
        private bool NguyenAm(char ch)
        {

            return (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y'||
                ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y');
        }
        private bool truoc_DauDup(char ch)
        {
            return (ch == 'a' || ch == 'd' || ch == 'o' || ch == 'e' || ch == 'u' ||
                ch == 'A' || ch == 'D' || ch == 'O' || ch == 'E' || ch == 'U');
        }
        private bool NguyenAmGanNhat(char ch)
        {

            return (
                ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' || ch == 'y' ||
                ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' || ch == 'Y' ||
                ch == 'â' || ch == 'ă' || ch == 'ê' || ch == 'ô' || ch == 'ơ' || ch == 'ư' ||
                ch == 'Â' || ch == 'Ă' || ch == 'Ê' || ch == 'Ô' || ch == 'Ơ' || ch == 'Ư');
        }
       

       

        
    }
}
